mod connection;
mod error;
mod handler;
mod protocol;
mod server;

use clap::Parser;
use std::net::SocketAddr;
use tracing::{error, info};
use tracing_subscriber;

#[derive(Parser, Debug)]
#[command(name = "rust-ws-bench")]
#[command(about = "High-performance WebSocket server", long_about = None)]
struct Args {
    #[arg(short, long, default_value = "127.0.0.1:8080")]
    addr: String,
    
    #[arg(short, long, default_value = "info")]
    log_level: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    
    // Initialize tracing
    let filter = tracing_subscriber::EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(&args.log_level));
    
    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_target(false)
        .init();
    
    info!("Starting high-performance WebSocket server");
    info!("Server address: {}", args.addr);
    info!("Log level: {}", args.log_level);
    
    // Parse address
    let addr: SocketAddr = args.addr.parse()?;
    
    // Create and run server
    let server = server::WebSocketServer::new(addr);
    
    // Run the server
    info!("Starting server...");
    if let Err(e) = server.run().await {
        error!("Server error: {}", e);
        std::process::exit(1);
    }
    
    Ok(())
}
